import { computed, onUnmounted, ref } from "vue"
import dayjs from "dayjs";

// 封装倒计时函数
export const countTime = () => {
    const time = ref(0)
    let timer;
    const start = (timeVal) => {
        time.value = timeVal
        timer = setInterval(() => {
            time.value--
        }, 1000)
    }
    // 格式化时间
    const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))

    onUnmounted(() => {
        timer && clearInterval(timer)
    })

    return {
        formatTime,
        start,
    }
}